package com.ciwong.epaper.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.NestedScrollView;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.epaper.bean.Result_scoce;
import com.ciwong.epaper.modules.epaper.util.ListenSpeakUtil;
import com.ciwong.epaper.util.ScoreUtils;
import com.ciwong.epaper.widget.toast.ZToast;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

/**
 * Created by young on 2017/4/10.
 */
public class SubmitSuccessfulResultPage extends NestedScrollView {

    public static final int DISPLAY_TYPE_LSW = 0;
    public static final int DISPLAY_TYPE_WORD = 1;
    public static final int DISPLAY_TYPE_SENTENCE = 2;
    public static final int DISPLAY_TYPE_ONLINE_ANSWER = 3;
    public static final int DISPLAY_TYPE_DICTATION = 4;
    private static final String TAG = "SubmitSuccessfulResultPage";
    /**
     * 结果页各个分数对应文字
     */
    private final static String[] SCORE_RESULT_TEXT = {"You can do better!",
            "Good!", "Well done!", "Wonderful!", "Awesome!"};
    /**
     * 得分
     */
    public RiseNumberTextView tx_score_result;

    //    private TextView tx_submit_success_tips;
    private TextView tx_title_text;
    private ImageView img_arrow_shuaxin;
    private TextView tx_work_time;
    private TextView btn_check_detail;
    public Button btn_return_last_page;
    /**
     * 总分
     */
    private TextView tx_work_score;
    private TextView countText; //句子个数
    private TextView tx_score_result_encourage;
    private LinearLayout rankLinear, mLlCheckDetail;

    private RelativeLayout handle_score_page;
    /**
     * 总分
     */
    private float mTotalScore;
    /**
     * 得分
     */
    private float mTestScore;

    private int mDisplayType = DISPLAY_TYPE_LSW;

    private TextView time_count_text;

    private TextView tx_check_later_tips;
    public TextView tv_submit_description;

    private View btn_ranking_list;


    /**
     * 排行榜跳转参数
     */
    private String mClientId;

    private String mUserId;

    private String mContentId;

    private boolean obj = false;


    public SubmitSuccessfulResultPage(Context context) {
        super(context);
        LayoutInflater.from(this.getContext()).inflate(R.layout.submit_successful_result_page, this);
        initView();
    }

    public SubmitSuccessfulResultPage(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(this.getContext()).inflate(R.layout.submit_successful_result_page, this);
        initView();
    }

    public SubmitSuccessfulResultPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(this.getContext()).inflate(R.layout.submit_successful_result_page, this);
        initView();
    }

    private TextView tv_corrections;
//    private View view_subjective;

    private void initView() {
        tx_score_result = (RiseNumberTextView) findViewById(R.id.tx_score_result);


//        tx_submit_success_tips = (TextView) findViewById(R.id.tx_submit_success_tips);
        img_arrow_shuaxin = (ImageView) findViewById(R.id.img_arrow_shuaxin);
        tx_work_time = (TextView) findViewById(R.id.tx_work_time);
        btn_check_detail = (TextView) findViewById(R.id.btn_check_detail);
        btn_return_last_page = (Button) findViewById(R.id.btn_return_last_page);
        tx_title_text = (TextView) findViewById(R.id.tx_title_text);
        time_count_text = (TextView) findViewById(R.id.time_count_text);
//        view_subjective = findViewById(R.id.view_subjective);

        tv_corrections = (TextView) findViewById(R.id.tv_corrections);

        btn_return_last_page.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity) getContext()).finish();
            }
        });

        tx_work_score = (TextView) findViewById(R.id.tx_work_score);
        countText = (TextView) findViewById(R.id.count_text);
        tx_score_result_encourage = (TextView) findViewById(R.id.tx_score_result_encourage);

        tx_check_later_tips = (TextView) findViewById(R.id.tx_check_later_tips);


        rankLinear = (LinearLayout) findViewById(R.id.rank_linear);

//
//        btn_ranking_list = findViewById(R.id.btn_ranking_list);

        tv_submit_description = (TextView) findViewById(R.id.tv_submit_description);

        handle_score_page = (RelativeLayout) findViewById(R.id.handle_score_page);
        mLlCheckDetail = (LinearLayout) findViewById(R.id.ll_check_detail);


        findViewById(R.id.iv_close_page).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                ((Activity) getContext()).finish();

            }
        });

    }

    public void setRankLinIsVis(int visibility) {
        if (rankLinear != null) {
            rankLinear.setVisibility(View.GONE);
            findViewById(R.id.rank_lin).setVisibility(View.GONE);
        }
    }


    public void setRankListPara(String clientId, String userId, String contentId) {

        mClientId = clientId;
        mUserId = userId;
        mContentId = contentId;
    }


    public void setSueButText(String text) {
        btn_return_last_page.setText(text);
    }

    public void setTimeTitleText(String title) {
        ((TextView) findViewById(R.id.time_count_text)).setText(title);
    }


    public void setCountText(String text) {
        countText.setText(text);
    }

    /**
     * 设置总分
     *
     * @param text
     */
    public void setScoreText(String text) {
        tx_work_score.setText(text);
    }

    /**
     * @param evaluateSuccess 评分是否成功
     * @param score           得分
     * @param lsMode
     */
    public void setEvaluateState(int type, boolean evaluateSuccess, String score, int lsMode, long nowTime, long effectiveDate, int showScoreMode, boolean serviceExpired) {
        if (type == 1) {//线上做答
            tx_score_result.setVisibility(GONE);
            tv_corrections.setVisibility(VISIBLE);
            tv_corrections.setText(getResources().getString(R.string.make_score_ing3));

            tx_score_result_encourage.setVisibility(GONE);
            tx_check_later_tips.setVisibility(GONE);
            img_arrow_shuaxin.setVisibility(VISIBLE);

        } else if (evaluateSuccess) {
            if (serviceExpired) {
                if (showScoreMode == 1 && nowTime < effectiveDate) {
                    tx_score_result.setText("*");
                    tv_corrections.setVisibility(GONE);
                    img_arrow_shuaxin.setVisibility(GONE);
                    tx_score_result.setVisibility(VISIBLE);
                } else {
                    mTestScore = new Float(score);
                    BigDecimal bd = new BigDecimal(score);
                    //小数位数
                    int decimalsCount = bd.scale();
                    tv_corrections.setVisibility(GONE);
                    tx_score_result.setVisibility(VISIBLE);
                    if (decimalsCount == 0) {
                        int intScore = (int) mTestScore;
                        tx_score_result.withNumber(intScore);
                    } else {
                        tx_score_result.withNumber(mTestScore);
                    }
                    // 得分用动画
                    tx_score_result.setDuration(1000);
                    tx_score_result.start();
                    img_arrow_shuaxin.setVisibility(GONE);
                }
                if (lsMode == ListenSpeakUtil.LsMode.SUBJECTIVE_MODE) { //主观题模式
                    tx_score_result.setVisibility(GONE);
                    tv_corrections.setVisibility(VISIBLE);
                    tv_corrections.setText(getResources().getString(R.string.make_score_ing3));
                    tx_score_result_encourage.setVisibility(GONE);

                } else {
                    //view_subjective.setVisibility(GONE);
                }
                tx_check_later_tips.setVisibility(GONE);
                if (mTotalScore > 0) {
                    if (lsMode == ListenSpeakUtil.LsMode.SUBJECTIVE_MODE) { //有主观题
                        tx_score_result_encourage.setVisibility(GONE);
                    } else {
                        tx_score_result_encourage.setVisibility(VISIBLE);
                    }
                    BigDecimal totalDec = new BigDecimal(mTotalScore);
                    BigDecimal testDec = new BigDecimal(mTestScore);

                    float percetn = testDec.divide(totalDec, 2, BigDecimal.ROUND_HALF_UP).floatValue();
                    percetn = percetn * 100;
                    int perScore = Float.valueOf(percetn).intValue();

                    if (perScore <= 60) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[0]);
                    } else if (perScore < 70 && perScore >= 60) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[1]);
                    } else if (perScore < 80 && perScore >= 70) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[2]);
                    } else if (perScore < 90 && perScore >= 80) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[3]);
                    } else if (perScore >= 90) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[4]);
                    }

                }
            } else {
                tx_score_result.setText("已完成");
                String text = "查看作业成绩";
                SpannableString spannableString = new SpannableString(text);
                spannableString.setSpan(new UnderlineSpan(), 0, text.length(), 0);
                tx_score_result_encourage.setText(spannableString);
                tv_corrections.setVisibility(GONE);
                img_arrow_shuaxin.setVisibility(GONE);
                tx_check_later_tips.setVisibility(GONE);
                tx_score_result.setVisibility(VISIBLE);
                tx_score_result_encourage.setVisibility(VISIBLE);
            }
        } else {

            if (lsMode == ListenSpeakUtil.LsMode.OWN_PRACTISE) {
                tx_check_later_tips.setText(getResources().getString(R.string.str_check_later_practice));
                tx_check_later_tips.setVisibility(VISIBLE);

            } else {
                tx_check_later_tips.setVisibility(VISIBLE);
                tx_check_later_tips.setText(getResources().getString(R.string.str_check_later_homework));
            }

            tx_score_result.setVisibility(GONE);
            tv_corrections.setVisibility(VISIBLE);
            if (obj) {
                tv_corrections.setText(getResources().getString(R.string.make_score_ing3));
            } else {
                tv_corrections.setText(getResources().getString(R.string.make_score_ing2));
                img_arrow_shuaxin.setVisibility(VISIBLE);
            }

        }
    }


    public void setEvaluateLastState(Result_scoce result_scoce, int showScoreMode, long nowTime, long effectiveDate,boolean serviceExpired) {
        if (result_scoce.getStatus() == 1 ||
                result_scoce.getStatus() == 4) {
            if (serviceExpired) {
                if (showScoreMode == 1 && nowTime < effectiveDate) {
                    tx_score_result.setText("*");
                    tx_score_result.setVisibility(VISIBLE);
                    tv_corrections.setVisibility(GONE);
                    img_arrow_shuaxin.setVisibility(GONE);
                    tx_check_later_tips.setVisibility(GONE);
                } else {
                    mTestScore = new Float(result_scoce.getActualScore());
                    BigDecimal bd = new BigDecimal(result_scoce.getActualScore());

                    //小数位数
                    int decimalsCount = bd.scale();
                    if (decimalsCount == 0) {
                        int intScore = (int) mTestScore;
                        tx_score_result.withNumber(intScore);
                    } else {
                        tx_score_result.withNumber(mTestScore);
                    }
                    // 得分用动画
                    tx_score_result.setDuration(1000);
                    tx_score_result.start();
                    tx_score_result.setVisibility(VISIBLE);
                    tv_corrections.setVisibility(GONE);
                    img_arrow_shuaxin.setVisibility(GONE);
                    tx_check_later_tips.setVisibility(GONE);

                    BigDecimal totalDec = new BigDecimal(mTotalScore);
                    BigDecimal testDec = new BigDecimal(mTestScore);
                    float percetn;
                    try {
                        percetn = testDec.divide(totalDec, 2, BigDecimal.ROUND_HALF_UP).floatValue();
                    } catch (Exception e) {
                        percetn = 0;
                    }
                    percetn = percetn * 100;
                    int perScore = Float.valueOf(percetn).intValue();

                    if (perScore <= 60) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[0]);
                    } else if (perScore < 70 && perScore >= 60) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[1]);
                    } else if (perScore < 80 && perScore >= 70) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[2]);
                    } else if (perScore < 90 && perScore >= 80) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[3]);
                    } else if (perScore >= 90) {
                        tx_score_result_encourage.setText(SCORE_RESULT_TEXT[4]);
                    }
                }
            } else {
                tx_score_result.setText("已完成");
                String text = "查看作业成绩";
                SpannableString spannableString = new SpannableString(text);
                spannableString.setSpan(new UnderlineSpan(), 0, text.length(), 0);
                tx_score_result_encourage.setText(spannableString);
                tv_corrections.setVisibility(GONE);
                img_arrow_shuaxin.setVisibility(GONE);
                tx_check_later_tips.setVisibility(GONE);
                tx_score_result.setVisibility(VISIBLE);
                tx_score_result_encourage.setVisibility(VISIBLE);
            }
        } else if (result_scoce.getStatus() == 16) {
            tx_score_result.setVisibility(GONE);
            tv_corrections.setVisibility(VISIBLE);
            tv_corrections.setText(getResources().getString(R.string.make_score_ing2));
            tx_score_result_encourage.setVisibility(GONE);
            tx_check_later_tips.setVisibility(VISIBLE);
            img_arrow_shuaxin.setVisibility(VISIBLE);

        } else {
            tx_score_result.setVisibility(GONE);
            tv_corrections.setVisibility(VISIBLE);
            tv_corrections.setText(getResources().getString(R.string.make_score_ing3));
            tx_score_result_encourage.setVisibility(GONE);
            tx_check_later_tips.setVisibility(VISIBLE);
            img_arrow_shuaxin.setVisibility(VISIBLE);
        }


    }

    public void goneshuaxin(boolean ishasobj) {
        if (ishasobj) {
            obj = ishasobj;
            img_arrow_shuaxin.setVisibility(GONE);
        } else {
            img_arrow_shuaxin.setVisibility(VISIBLE);
        }

    }

    /**
     * 设置总分
     *
     * @param score
     */
    public void setToalScore(float score) {
        mTotalScore = score;
        tx_work_score.setText(ScoreUtils.getScoreDisFormat(score) + "分");
    }

    public void setWorkTime(String workTime) {
        tx_work_time.setText(workTime);
    }

    public void setTitleText(String titleText) {
        tx_title_text.setText(titleText);
    }


    public void setOnRefreshScoreListener(OnClickListener listener) {
        img_arrow_shuaxin.setOnClickListener(listener);
    }

    public void setOnCheckDetailListener(OnClickListener listener) {
        mLlCheckDetail.setOnClickListener(listener);
    }

    public void setOnCheckTipsListener(OnClickListener listener) {
        tx_score_result_encourage.setOnClickListener(listener);
    }

    public void setOnCheckRankingListListener(OnClickListener listListener) {
        btn_ranking_list.setOnClickListener(listListener);
    }


    public void setOnToRankListener(OnClickListener listener) {
        if (rankLinear != null) {
            rankLinear.setOnClickListener(listener);
        }
    }


    /**
     * @param type 单词跟读，句子跟读，听说模考
     */
    public void setDispayType(int type) {

        switch (type) {

            case DISPLAY_TYPE_LSW:
                time_count_text.setText(getResources().getString(R.string.str_use_time_answer));
                countText.setText("总    分");
                break;

            case DISPLAY_TYPE_WORD:
                time_count_text.setText(getResources().getString(R.string.str_use_time_follow));
                countText.setText("单词个数");
                break;

            case DISPLAY_TYPE_SENTENCE:
                time_count_text.setText(getResources().getString(R.string.str_use_time_follow));
                countText.setText("句子个数");
                break;
            case DISPLAY_TYPE_ONLINE_ANSWER:
                countText.setText("总    分");
                time_count_text.setText(getResources().getString(R.string.str_use_time_answer));
                break;
            case DISPLAY_TYPE_DICTATION:
                countText.setText("总    分");
                time_count_text.setText(getResources().getString(R.string.str_use_time_answer));
                break;
        }
    }

    public CharSequence wordDyeing(String beforText, String afterText) {
        ForegroundColorSpan colorSpanBefor = new ForegroundColorSpan(Color.parseColor("#F67177"));
        ForegroundColorSpan colorSpanAfter = new ForegroundColorSpan(Color.parseColor("#52CC8F"));
        SpannableStringBuilder spannableString = new SpannableStringBuilder(beforText);
        spannableString.setSpan(colorSpanBefor, beforText.indexOf("次") - 1, beforText.indexOf("次"), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.append(afterText);
        spannableString.setSpan(colorSpanAfter, spannableString.length() - 2, spannableString.length() - 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}
